package org.wjw.auth.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.wjw.auth.WjwPermission;
import org.wjw.auth.domain.Role;
import org.wjw.auth.service.IRoleService;
import org.wjw.base.util.AjaxResult;
import org.wjw.dto.accept.MenuDTO;
import org.wjw.dto.accept.PermissionDTO;
import org.wjw.dto.accept.ResultDTO;
import org.wjw.base.query.BaseQuery;

import java.util.List;

@RestController
@RequestMapping("/role")
@Api(value = "角色管理",tags = "角色管理")
@WjwPermission(name="角色管理",desc = "我是角色管理")
public class RoleController {
    @Autowired
    IRoleService roleService;

    @PutMapping
    @ApiOperation(value = "添加或者更新方法")
    @WjwPermission(name="添加或者修改",desc = "我是添加或者修改")
    public AjaxResult addOrUpdate(@RequestBody Role role){
        try {
            if(role.getId()!=null){
                roleService.update(role);
            }else {
                roleService.insert(role);
            }
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("操作出错"+e.getMessage());
        }
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除方法")
    @WjwPermission(name="删除",desc = "我是删除")
    public AjaxResult delete(@PathVariable("id") @ApiParam(value = "主键",required = true) Long id){
        try {
            roleService.deleteById(id);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个角色方法")
    @WjwPermission(name="通过id查询",desc = "我是通过id查询")
    public AjaxResult getById(@ApiParam(value = "主键",required = true) @PathVariable("id") Long id){
        try {
            Role role = roleService.selectById(id);
            return new AjaxResult().getself().setObj(role);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }

    @GetMapping("/sn/{roleId}")
    @ApiOperation(value = "通过roleId查询权限")
    @WjwPermission(name="通过roleId查询权限",desc = "我是通通过roleId查询权限")
    public AjaxResult getSnByRowId(@ApiParam(value = "主键",required = true) @PathVariable("roleId") Long roleId){
        try {
            List<String> sns = roleService.getSnByRowId(roleId);
            return new AjaxResult().getself().setObj(sns);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }


    @GetMapping("/id/{roleId}")
    @ApiOperation(value = "通过roleId查询menuId")
    @WjwPermission(name="通过roleId查询menuId",desc = "我是通过roleId查询menuId")
    public AjaxResult getMenuIdByRowId(@ApiParam(value = "主键",required = true) @PathVariable("roleId") Long roleId){
        try {
            List<Long> ids = roleService.getMenuIdByRowId(roleId);
            return new AjaxResult().getself().setObj(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }


    @GetMapping
    @ApiOperation(value = "查询所有角色方法")
    public AjaxResult getAll(){
        try {
            List<Role> roles = roleService.getAll();
            return new AjaxResult().getself().setObj(roles);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("查询出错"+e.getMessage());
        }
    }


    @PostMapping
    @ApiOperation(value = "分页查询角色方法")
    public AjaxResult getPage(@RequestBody BaseQuery queryVo){
        try {
            System.out.println(queryVo);
            ResultDTO<Role> result = roleService.getAllPage(queryVo);
            return new AjaxResult().getself().setObj(result);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("分页查询出错"+e.getMessage());
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除方法")
    public AjaxResult batchDelete(@ApiParam(value = "主键",required = true)@RequestBody List<Long> ids){
        try {
            System.out.println("批量删除方法");
            roleService.batchDelete(ids);
            return new AjaxResult().getself();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMessage("删除出错"+e.getMessage());
        }
    }

    @PostMapping("/permission")
    public AjaxResult setRolePermisison(@RequestBody PermissionDTO dto){
        try {
            System.out.println(dto.getPermissionSns());
            roleService.setRolePermisison(dto);
            return AjaxResult.getself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }


    @PostMapping("/menu")
    public AjaxResult setRoleMenu(@RequestBody MenuDTO dto){
        try {
            roleService.setRoleMenu(dto);
            return AjaxResult.getself();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("设置权限失败!"+e.getMessage());
        }
    }


    @GetMapping("/ids")
    public AjaxResult getAllRoleId(){
        try {
            List<Long> roleIds = roleService.getAllRoleId();
            return AjaxResult.getself().setObj(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }


    @GetMapping("/list")
    public AjaxResult getRoles(){
        try {
            List<Role> roleIds = roleService.getRoles();
            return AjaxResult.getself().setObj(roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getself().setSuccess(false).setMessage("角色列表查询失败!"+e.getMessage());
        }
    }
}
